﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QA_System
{
    public class NPAnaphoraResolve
    {
        static private bool iresolve;
        static private string mixNounThey="";
        static private string mixNounWe = "Tôi, ";

        static public XMLNounConnection conn = new XMLNounConnection();

        public NPAnaphoraResolve()
        {
            iresolve = false;
        }

        static public bool Iresolve
        {
            get {return iresolve; }
        }

        static public void ResolveIntraSimple(SentenceStruct ssPresen,ref SentenceStruct ssSen)
        {
            try
            {
                iresolve = false;
                int[] inAllPronoun = ssSen.GetProNounsPosition(); 
                int[] inAllNoun = ssPresen.GetNounsPosition(); 
                int i = 0;
                for (i = 0; i < inAllPronoun.Length; i++)
                {
                    int j = 0;
                    for (j = 0; j < inAllNoun.Length; j++)
                    {
                        if (NPanaphoraCondition.INumberAgree(ssSen[ssSen[inAllPronoun[i]].Ower],ssPresen[inAllNoun[j]]))
                        {
                            if (NPanaphoraCondition.IGenderAgree(ssSen[ssSen[inAllPronoun[i]].Ower], ssPresen[inAllNoun[j]]))
                            {
                                if(inAllPronoun[i]==0)
                                {
                                    int index = ssSen.GetVerbPosition(0, ssSen.SentenceLength);
                                    if (index != -1)
                                    {
                                        WordStruct wsVerb = ssSen[index];
                                        if (wsVerb.Word.Equals("là")&&(wsVerb.Word.Equals("được")))
                                        {
                                            ssSen[inAllPronoun[i]].Pos = ssPresen[inAllNoun[j]].Pos;
                                            ssSen[inAllPronoun[i]].Word = ssPresen[inAllNoun[j]].Word;

                                            break;
                                        }
                                        else
                                        {
                                            if ((NPanaphoraCondition.IVerbAgree(ssPresen[inAllNoun[j]], wsVerb, false)))
                                            {
                                                ssSen[inAllPronoun[i]].Pos = ssPresen[inAllNoun[j]].Pos;
                                                ssSen[inAllPronoun[i]].Word = ssPresen[inAllNoun[j]].Word;

                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        ssSen[inAllPronoun[i]].Pos = ssPresen[inAllNoun[j]].Pos;
                                        ssSen[inAllPronoun[i]].Word = ssPresen[inAllNoun[j]].Word;

                                        break;
                                    }
                                }
                                else
                                {
                                    WordStruct wsVerb = null;
                                    int index = ssSen.GetVerbPosition(inAllPronoun[i], ssSen.SentenceLength);
                                    if(index!=-1) 
                                    {
                                        wsVerb = ssSen[index];
                                    
                                        if (NPanaphoraCondition.IVerbAgree(ssPresen[inAllNoun[j]], wsVerb, false))
                                        {
                                            index = ssSen.GetLastVerbPosition(0, inAllPronoun[i]);
                                            if (index != -1)
                                            {
                                                wsVerb = ssSen[index];

                                                if (NPanaphoraCondition.IVerbAgree(ssPresen[inAllNoun[j]], wsVerb, true))
                                                {
                                                    ssSen[inAllPronoun[i]].Pos = ssPresen[inAllNoun[j]].Pos;
                                                    ssSen[inAllPronoun[i]].Word = ssPresen[inAllNoun[j]].Word;
                                                    break;
                                                }
                                            }
                                            else
                                            {
                                                ssSen[inAllPronoun[i]].Pos = ssPresen[inAllNoun[j]].Pos;
                                                ssSen[inAllPronoun[i]].Word = ssPresen[inAllNoun[j]].Word;
                                                break;
                                            }
                                        }
                                     }
                                     else
                                      {
                                          index = ssSen.GetLastVerbPosition(0, inAllPronoun[i]);
                                          if (index != -1)
                                          {
                                              wsVerb = ssSen[index];

                                              if ((wsVerb == null) || (NPanaphoraCondition.IVerbAgree(ssPresen[inAllNoun[j]], wsVerb, true)))
                                              {
                                                  ssSen[inAllPronoun[i]].Pos = ssPresen[inAllNoun[j]].Pos;
                                                  ssSen[inAllPronoun[i]].Word = ssPresen[inAllNoun[j]].Word;
                                                  break;
                                              }
                                          }
                                          else
                                          {
                                              ssSen[inAllPronoun[i]].Pos = ssPresen[inAllNoun[j]].Pos;
                                              ssSen[inAllPronoun[i]].Word = ssPresen[inAllNoun[j]].Word;
                                              break;
                                          }
                                    }
                                }
                            }

                        }
                    }
                  //  if (ssSen[inAllPronoun[i]].Ower != i) ssSen[ssSen[inAllPronoun[i]].Ower].Pos = "--";
                }
                inAllPronoun = ssSen.GetProNounsPosition();
                if (inAllPronoun.Length == 0) iresolve = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        static public void ResolveIntraComplex(SentenceStruct ssPresen,ref SentenceStruct ssSen)
        {
            try
            {
                iresolve = false;
                int[] humanPronounPosition = ssSen.GetProNounsPosition();

                int[] preHumanPronounPosition = ssPresen.GetProNounsPosition();

                if (humanPronounPosition.Length != preHumanPronounPosition.Length) iresolve = false;
                else
                {
                    int restotal = humanPronounPosition.Length * preHumanPronounPosition.Length;
                    bool[,] board = new bool[restotal, 6];
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        static public void ResolveInter(ref SentenceStruct ssSen)
        {
            try
            {
                iresolve = false;
                int[] inAllPronoun = ssSen.GetProNounsPosition();
                int[] inAllNoun = ssSen.GetNounsPosition();
                int i = 0;
                for (i = 0; i < inAllPronoun.Length; i++)
                {
                    int j = 0;
                    for (j = 0; j < inAllNoun.Length; j++)
                    {
                        if (NPanaphoraCondition.INumberAgree(ssSen[ssSen[inAllPronoun[i]].Ower], ssSen[inAllNoun[j]]))
                        {
                            if (NPanaphoraCondition.IGenderAgree(ssSen[ssSen[inAllPronoun[i]].Ower], ssSen[inAllNoun[j]]))
                            {
                                if (inAllPronoun[i] == 0)
                                {
                                    int index = ssSen.GetVerbPosition(0, ssSen.SentenceLength);
                                    if (index != -1)
                                    {
                                        WordStruct wsVerb = ssSen[index];
                                        if (wsVerb.Word.Equals("là") && (wsVerb.Word.Equals("được")))
                                        {
                                            ssSen[inAllPronoun[i]].Pos = ssSen[inAllNoun[j]].Pos;
                                            ssSen[inAllPronoun[i]].Word = ssSen[inAllNoun[j]].Word;

                                            break;
                                        }
                                        else
                                        {
                                            if ((NPanaphoraCondition.IVerbAgree(ssSen[inAllNoun[j]], wsVerb, false)))
                                            {
                                                ssSen[inAllPronoun[i]].Pos = ssSen[inAllNoun[j]].Pos;
                                                ssSen[inAllPronoun[i]].Word = ssSen[inAllNoun[j]].Word;

                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        ssSen[inAllPronoun[i]].Pos = ssSen[inAllNoun[j]].Pos;
                                        ssSen[inAllPronoun[i]].Word = ssSen[inAllNoun[j]].Word;

                                        break;
                                    }
                                }
                                else
                                {
                                    WordStruct wsVerb = null;
                                    int index = ssSen.GetVerbPosition(inAllPronoun[i], ssSen.SentenceLength);
                                    if (index != -1)
                                    {
                                        wsVerb = ssSen[index];

                                        if (NPanaphoraCondition.IVerbAgree(ssSen[inAllNoun[j]], wsVerb, false))
                                        {
                                            index = ssSen.GetLastVerbPosition(0, inAllPronoun[i]);
                                            if (index != -1)
                                            {
                                                wsVerb = ssSen[index];

                                                if (NPanaphoraCondition.IVerbAgree(ssSen[inAllNoun[j]], wsVerb, true))
                                                {
                                                    ssSen[inAllPronoun[i]].Pos = ssSen[inAllNoun[j]].Pos;
                                                    ssSen[inAllPronoun[i]].Word = ssSen[inAllNoun[j]].Word;
                                                    break;
                                                }
                                            }
                                            else
                                            {
                                                ssSen[inAllPronoun[i]].Pos = ssSen[inAllNoun[j]].Pos;
                                                ssSen[inAllPronoun[i]].Word = ssSen[inAllNoun[j]].Word;
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        index = ssSen.GetLastVerbPosition(0, inAllPronoun[i]);
                                        if (index != -1)
                                        {
                                            wsVerb = ssSen[index];

                                            if ((NPanaphoraCondition.IVerbAgree(ssSen[inAllNoun[j]], wsVerb, true)))
                                            {
                                                ssSen[inAllPronoun[i]].Pos = ssSen[inAllNoun[j]].Pos;
                                                ssSen[inAllPronoun[i]].Word = ssSen[inAllNoun[j]].Word;
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            ssSen[inAllPronoun[i]].Pos = ssSen[inAllNoun[j]].Pos;
                                            ssSen[inAllPronoun[i]].Word = ssSen[inAllNoun[j]].Word;
                                            break;
                                        }
                                    }
                                }
                            }

                        }
                    }
                  //  if (ssSen[inAllPronoun[i]].Ower != i) ssSen[ssSen[inAllPronoun[i]].Ower].Pos = "--";
                }
                inAllPronoun = ssSen.GetProNounsPosition();
                if (inAllPronoun.Length == 0) iresolve = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        static public sentence_tagger Resolve(SentenceStruct ssPresen, SentenceStruct ssSen, int countStack)
        {
            try
            {
                sentence_tagger resultSen = null;
                int[] allPronounPo = ssSen.GetProNounsPosition();
                int[] allNoun = ssPresen.GetNounsPosition();
                string strRword = "được,do,bị";
                if ((allNoun.Length >= 0) && (allPronounPo.Length >= 0))
                {
                    resultSen = new sentence_tagger(ssSen.SentenceLength);
                    #region place
                    for (int index = 0; index < allPronounPo.Length; index++)
                    {
                        if (ssSen[allPronounPo[index]].Word.ToLower().Equals("đó") || (ssSen[allPronounPo[index]].Word.ToLower().Equals("đấy")))
                        {
                            if (ssSen[allPronounPo[index] - 1].Pos.Equals("V") || ssSen[allPronounPo[index] - 1].Pos.Equals("E"))
                            {
                                for (int j = 0; j < allNoun.Length; j++)
                                {
                                    if (allNoun[j] > 0)
                                    {

                                        if (conn.GetWordType(ssPresen[allNoun[j]], "").Equals("Place") || conn.GetWordType(ssPresen[allNoun[j]], "").Equals("BuildingConstruction") || ssPresen[allNoun[j] - 1].Pos.Equals("E"))
                                        {
                                            ssSen[allPronounPo[index]].Word = ssPresen[allNoun[j]].Word;
                                            ssSen[allPronounPo[index]].Pos = ssPresen[allNoun[j]].Pos;
                                            break;
                                        }
                                    }
                                    if (conn.GetWordType(ssPresen[0], "").Equals("Place") || conn.GetWordType(ssPresen[0], "").Equals("BuildingConstruction"))
                                    {
                                        ssSen[allPronounPo[index]].Word = ssPresen[0].Word;
                                        ssSen[allPronounPo[index]].Pos = ssPresen[0].Pos;
                                        iresolve = true;
                                        break;
                                        
                                    }
                                }
                            }
                         }
                        //TH P là họ
                        else if ((ssSen[allPronounPo[index]].Word.ToLower().Equals("họ")))
                        {
                            int[] allHumanNouns = ssPresen.GetHumanNounsPosition();
                            //string mixNoun="";
                            for (int i = 0; i < allHumanNouns.Length; i++)
                            {
                                if (!mixNounThey.Contains(ssPresen[allHumanNouns[i]].Word))
                                {
                                    mixNounThey = mixNounThey + ssPresen[allHumanNouns[i]].Word;
                                    mixNounThey = mixNounThey + ", ";
                                }
                                
                               
                                    
                                                                
                            }


                            if (countStack == 0)
                            {
                                mixNounThey = mixNounThey.Substring(0, mixNounThey.Length - 2);
                                ssSen[allPronounPo[index]].Word = mixNounThey;
                                ssSen[allPronounPo[index]].Pos = "";
                                mixNounThey = "";
                                iresolve = true;
                            }
                        }
                        //Trường hợp là "chúng tôi, chúng ta"
                        else if (ssSen[allPronounPo[index]].Word.ToLower().Equals("chúng tôi") || ssSen[allPronounPo[index]].Word.ToLower().Equals("chúng ta"))
                        {
                            int[]  allHumanNouns = ssPresen.GetHumanNounsPosition();
                            /*
                            string mixNoun = "tôi, ";
                            for (int i = 0; i < allHumanNouns.Length; i++)
                            {
                                mixNoun = mixNoun + ssPresen[allHumanNouns[i]].Word;
                                if (i != allHumanNouns.Length - 1)
                                    mixNoun = mixNoun + ", ";

                            }
                            ssSen[allPronounPo[index]].Word = mixNoun;
                            ssSen[allPronounPo[index]].Pos = "";
                            iresolve = true;
                             * */

                            for (int i = 0; i < allHumanNouns.Length; i++)
                            {
                                if (!mixNounWe.Contains(ssPresen[allHumanNouns[i]].Word))
                                {
                                    mixNounWe = mixNounWe + ssPresen[allHumanNouns[i]].Word;
                                    mixNounWe = mixNounWe + ", ";
                                }




                            }


                            if (countStack == 0)
                            {
                                mixNounWe = mixNounWe.Substring(0, mixNounWe.Length - 2);
                                ssSen[allPronounPo[index]].Word = mixNounWe;
                                ssSen[allPronounPo[index]].Pos = "";
                                mixNounWe = "";
                                iresolve = true;
                            }
                        }

                    }
#endregion
                    allPronounPo = ssSen.GetProNounsPosition();
                    if (allPronounPo.Length > 0)
                    {
                        
                        if (allPronounPo.Length == 1)
                        {
                            #region 1pronoun
                            for (int j = 0; j < allNoun.Length; j++)
                            {
                                if(NPanaphoraCondition.INumberAgree(ssSen[allPronounPo[0]],ssPresen[allNoun[j]]))
                                {
                                    if (NPanaphoraCondition.IGenderAgree(ssSen[allPronounPo[0]], ssPresen[allNoun[j]]))
                                    {
                                        bool ok = false;
                                        int verbpo = ssSen.GetVerbPosition(allPronounPo[0]+1, ssSen.SentenceLength-1);
                                        if (verbpo != -1)
                                        {
                                            int k = 0;
                                            bool isPassive = false;
                                            for (k = allPronounPo[0] + 1; k<verbpo;k++)
                                                if (strRword.Contains(ssSen[k].Word.ToLower()))
                                                {                                                    
                                                    isPassive = true;
                                                    break;
                                                }
                                            if (isPassive)
                                            {
                                                ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], true);
                                            }
                                            else
                                                ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], false);
                                        }
                                        else ok = true;
                                        if (ok)
                                        {
                                            verbpo = ssSen.GetLastVerbPosition(0, allPronounPo[0]);
                                            if (verbpo != -1)
                                            {
                                                //cu la true
                                                //moi sua thanh false
                                                ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], true);
                                                //ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], false);
                                            }
                                            else ok = true;
                                            if (ok)
                                            {
                                                ssSen[allPronounPo[0]].Word = ssPresen[allNoun[j]].Word;
                                                ssSen[allPronounPo[0]].Pos = ssPresen[allNoun[j]].Pos;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        #endregion
                        }
                        else
                        {
                            int thingCount = 0;
                            int humanCount = 0;
                            for (int j = 0; j < allPronounPo.Length; j++)
                            {
                                if (ssSen[allPronounPo[j]].WordSubMean.Contains("People") && !ssSen[allPronounPo[j]].Word.ToLower().Equals("nó"))
                                {
                                    humanCount++;
                                }
                                else thingCount++;
                            }
                            #region thing
                            for (int i = 0; i < allPronounPo.Length; i++)
                            {
                                for (int j = 0; j < allNoun.Length; j++)
                                {
                                    if (ssPresen[allNoun[j]].WordSubMean.Contains("Thing")&&(ssSen[allPronounPo[i]].WordSubMean.Contains("Thing")))
                                    {
                                        if (NPanaphoraCondition.INumberAgree(ssSen[allPronounPo[i]], ssPresen[allNoun[j]]))
                                        {

                                            bool ok = false;
                                            int verbpo = ssSen.GetVerbPosition(allPronounPo[i] + 1, ssSen.SentenceLength - 1);
                                            if (verbpo != -1)
                                            {
                                                if (ssSen[verbpo - 1].Pos.Equals("R") && strRword.Contains(ssSen[verbpo - 1].Word.ToLower()))
                                                {
                                                    ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], true);
                                                }
                                                else
                                                    ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], false);
                                            }
                                            else ok = true;
                                            if (ok)
                                            {
                                                verbpo = ssSen.GetLastVerbPosition(0, allPronounPo[i]);
                                                if (verbpo != -1)
                                                {

                                                    ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], true);

                                                }
                                                else ok = true;
                                                if (ok)
                                                {
                                                    ssSen[allPronounPo[i]].Word = ssPresen[allNoun[j]].Word;
                                                    ssSen[allPronounPo[i]].Pos = ssPresen[allNoun[j]].Pos;
                                                    break;
                                                }
                                            }
                                        }

                                    }
                                }
                            }
                            #endregion
                            #region human
                            /*
                            if (humanCount == 1)
                            {
                                #region 1pronounhuman
                                for (int j = 0; j < allNoun.Length; j++)
                                {
                                    if (NPanaphoraCondition.INumberAgree(ssSen[allPronounPo[0]], ssPresen[allNoun[j]]))
                                    {
                                        if (NPanaphoraCondition.IGenderAgree(ssSen[allPronounPo[0]], ssPresen[allNoun[j]]))
                                        {
                                            bool ok = false;
                                            int verbpo = ssSen.GetVerbPosition(allPronounPo[0] + 1, ssSen.SentenceLength - 1);
                                            if (verbpo != -1)
                                            {
                                                if (ssSen[verbpo - 1].Pos.Equals("R") && strRword.Contains(ssSen[verbpo - 1].Word.ToLower()))
                                                {
                                                    ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], true);
                                                }
                                                else
                                                    ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], false);
                                            }
                                            else ok = true;
                                            if (ok)
                                            {
                                                verbpo = ssSen.GetLastVerbPosition(0, allPronounPo[0]);
                                                if (verbpo != -1)
                                                {

                                                    ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], true);

                                                }
                                                else ok = true;
                                                if (ok)
                                                {
                                                    ssSen[allPronounPo[0]].Word = ssPresen[allNoun[j]].Word;
                                                    ssSen[allPronounPo[0]].Pos = ssPresen[allNoun[j]].Pos;
                                                    iresolve = true;
                                                    break;                                                    
                                                }
                                            }
                                        }
                                    }
                                }
                             * */

                            if (humanCount>0)
                            {
                                
                            
                                #region 1pronounhuman
                                for (int i = 0; i < allPronounPo.Length; i++)
                                {
                                    for (int j = 0; j < allNoun.Length; j++)
                                    {
                                        if (NPanaphoraCondition.INumberAgree(ssSen[allPronounPo[i]], ssPresen[allNoun[j]]))
                                        {
                                            if (NPanaphoraCondition.IGenderAgree(ssSen[allPronounPo[i]], ssPresen[allNoun[j]]))
                                            {
                                                bool ok = false;
                                                int verbpo = ssSen.GetVerbPosition(allPronounPo[i] + 1, ssSen.SentenceLength - 1);
                                                if (verbpo != -1)
                                                {
                                                    if (ssSen[verbpo - 1].Pos.Equals("R") && strRword.Contains(ssSen[verbpo - 1].Word.ToLower()))
                                                    {
                                                        ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], true);
                                                    }
                                                    else
                                                        ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], false);
                                                }
                                                else ok = true;
                                                if (ok)
                                                {
                                                    verbpo = ssSen.GetLastVerbPosition(0, allPronounPo[i]);
                                                    if (verbpo != -1)
                                                    {
                                                        //Vinh them TH: Day la buc tranh dep nhat cua anh ay. Dong tu: la, P ở cuối câu
                                                        //chuyển parameters cuối từ true-> false
                                                        //f (ssSen[verbpo].Word=="là")
                                                        //    ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], false);
                                                        //else
                                                            ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], false);

                                                    }
                                                    else ok = true;
                                                    if (ok)
                                                    {
                                                        ssSen[allPronounPo[i]].Word = ssPresen[allNoun[j]].Word;
                                                        ssSen[allPronounPo[i]].Pos = ssPresen[allNoun[j]].Pos;
                                                        iresolve = true;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                                #endregion
                            
                            else
                            if(humanCount>1)
                            {
                                //Vinh them
                                /*
                                for (int j = 0; j < allNoun.Length; j++)
                                {
                                    if (NPanaphoraCondition.INumberAgree(ssSen[allPronounPo[0]], ssPresen[allNoun[j]]))
                                    {
                                        if (NPanaphoraCondition.IGenderAgree(ssSen[allPronounPo[0]], ssPresen[allNoun[j]]))
                                        {
                                            bool ok = false;
                                            int verbpo = ssSen.GetVerbPosition(allPronounPo[0] + 1, ssSen.SentenceLength - 1);
                                            if (verbpo != -1)
                                            {
                                                if (ssSen[verbpo - 1].Pos.Equals("R") && strRword.Contains(ssSen[verbpo - 1].Word.ToLower()))
                                                {
                                                    ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], true);
                                                }
                                                else
                                                    ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], false);
                                            }
                                            else ok = true;
                                            if (ok)
                                            {
                                                verbpo = ssSen.GetLastVerbPosition(0, allPronounPo[0]);
                                                if (verbpo != -1)
                                                {

                                                    ok = NPanaphoraCondition.IVerbAgree(ssPresen[allNoun[j]], ssSen[verbpo], true);

                                                }
                                                else ok = true;
                                                if (ok)
                                                {
                                                    ssSen[allPronounPo[0]].Word = ssPresen[allNoun[j]].Word;
                                                    ssSen[allPronounPo[0]].Pos = ssPresen[allNoun[j]].Pos;
                                                    iresolve = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                                 * */
                            }
                                 
                            #endregion
                        
                        }
                    }
                    //TH giai quyet P là họ

                    for (int index = 0; index < resultSen.Sentence_length; index++)
                    {
                        resultSen[index].Word_pos = ssSen[index].Pos;
                        resultSen[index].Word_text = ssSen[index].Word;
                    }
                }
                int [] pposition = ssSen.GetProNounsPosition();
                if (pposition.Length == 0) iresolve = true;
                return resultSen;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    public class VPAnaphoraResolve
    {
       static  private bool iresolve;

       static private XMLVerbConnection verbconn = new XMLVerbConnection();

       public VPAnaphoraResolve()
        {
            iresolve = false;
        }

        static public bool Iresolve
        {
            get { return iresolve; }
        }
        
        static public sentence_tagger Resolve(SentenceStruct ssPresen,SentenceStruct ssSen)
        {
            try
            {
                iresolve = false;
                int[] allpreVerbpoiton = ssSen.GetAllPreVerbPosition();
                int[] allVerbInPreSenPo = ssPresen.GetAllVerbPosition();
                sentence_tagger resultSen = null;
                if ((allpreVerbpoiton.Length > 0)&&(allVerbInPreSenPo.Length>0))
                {
                    switch (ssPresen[ssPresen.SentenceLength - 1].Word)
                    {
                        case ".":
                           
                                for (int i = 0; i < allpreVerbpoiton.Length; i++)
                                {
                                    if (!ssSen[allpreVerbpoiton[i] + 1].Pos.Equals("V"))
                                    {
                                        resultSen = new sentence_tagger(allpreVerbpoiton[i] + ssPresen.MeanwordLengh() - allVerbInPreSenPo[0]+1);
                                        for (int index = 0; index <= allpreVerbpoiton[i]; index++)
                                        {
                                            resultSen[index].Word_pos = ssSen[index].Pos;
                                            resultSen[index].Word_text = ssSen[index].Word;
                                        }
                                        int k = allpreVerbpoiton[i] + 1;
                                        
                                        for ( int index = allVerbInPreSenPo[i]; index < ssPresen.MeanwordLengh(); index++)
                                        {
                                            //Vinh sua
                                            //if (ssPresen[index].Pos.Equals("-"))
                                            //{
                                           // if (NPanaphoraCondition.IVerbAgree(ssPresen[inAllNoun[j]], wsVerb, false)
                                                resultSen[k].Word_text = ssPresen[index].Word;
                                                resultSen[k].Word_pos = ssPresen[index].Pos;
                                                k++;
                                            //}
                                        }
                                        iresolve = true;
                                        break;
                                    }
                                    else
                                    {
                                        for (int j = 0; j < allVerbInPreSenPo.Length; j++)
                                        {
                                            if (verbconn.IsBsameCatagoryA(ssSen[allpreVerbpoiton[i]+1],ssPresen[allVerbInPreSenPo[j]]))
                                            {
                                                resultSen = new sentence_tagger(allpreVerbpoiton[i] + ssPresen.SentenceLength - allVerbInPreSenPo[0]+1);
                                                for (int index = 0; index <= allpreVerbpoiton[i]; index++)
                                                {
                                                    resultSen[index].Word_pos = ssSen[index].Pos;
                                                    resultSen[index].Word_text = ssSen[index].Word;
                                                }
                                                int k = allpreVerbpoiton[i] + 1;
                                                for (int index = allVerbInPreSenPo[i]; index < ssPresen.SentenceLength; index++)
                                                {
                                                    resultSen[k].Word_text = ssPresen[index].Word;
                                                    resultSen[k].Word_pos = ssPresen[index].Pos;
                                                    k++;
                                                }
                                                iresolve = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                            
                           
                            break;
                        case "?":
                            if (ssSen[ssSen.SentenceLength - 1].Word.Equals("."))
                            {
                                for (int i = 0; i < allpreVerbpoiton.Length; i++)
                                {
                                    if (!ssSen[allpreVerbpoiton[i] + 1].Pos.Equals("V"))
                                    {
                                        resultSen = new sentence_tagger(allpreVerbpoiton[i] + ssPresen.SentenceLength - allVerbInPreSenPo[0]+1);
                                        for (int index = 0; index <= allpreVerbpoiton[i]; index++)
                                        {
                                            
                                            resultSen[index].Word_pos = ssSen[index].Pos;
                                            resultSen[index].Word_text = ssSen[index].Word;
                                           
                                        }
                                        int k = allpreVerbpoiton[i] + 1;
                                        for (int index = allVerbInPreSenPo[i]; index < ssPresen.SentenceLength; index++)
                                        {
                                            if (!ssPresen[k].Pos.Equals("-")) resultSen[k].Word_text = ssPresen[index].Word;
                                            else resultSen[k].Word_text = "";
                                            resultSen[k].Word_pos = ssPresen[index].Pos;
                                            k++;
                                        }
                                        iresolve = true;
                                        break;
                                    }
                                    else
                                    {
                                        for (int j = 0; j < allVerbInPreSenPo.Length; j++)
                                        {
                                            if (verbconn.IsBsameCatagoryA(ssSen[allpreVerbpoiton[i] + 1], ssPresen[allVerbInPreSenPo[j]]))
                                            {
                                                resultSen = new sentence_tagger(allpreVerbpoiton[i] + ssPresen.SentenceLength - allVerbInPreSenPo[0] + 1);
                                                for (int index = 0; index <= allpreVerbpoiton[i]; index++)
                                                {
                                                    resultSen[index].Word_pos = ssSen[index].Pos;
                                                    resultSen[index].Word_text = ssSen[index].Word;
                                                }
                                                int k = allpreVerbpoiton[i] + 1;
                                                for (int index = allVerbInPreSenPo[i]; index < ssPresen.SentenceLength; index++)
                                                {
                                                    resultSen[k].Word_text = ssPresen[index].Word;
                                                    resultSen[k].Word_pos = ssPresen[index].Pos;
                                                    k++;
                                                }
                                                iresolve = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {

                            }
                            break;
                           
                    }
               }
                return resultSen;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        
    }

    public class SAnaphoraResolve
    {
        static private bool iresolve;
        static string xmlDatabasePath = QA_System.Properties.Settings.Default.xmlDatabasePath;
        public SAnaphoraResolve()
        {
            iresolve = false;
        }

        static public bool Iresolve
        {
            get { return iresolve; }
        }

        static public sentence_tagger Resolve(SentenceStruct ssPresen, SentenceStruct ssSen)
        {
            try
            {
                iresolve = false;
                sentence_tagger senresult = null;
                //Vinh sua
                SAXMLConnection SAna = new SAXMLConnection();
                for (int k = 0; k < ssSen.SentenceLength - 1; k++ )
                {

                    if (SAna.IsSA(ssSen[k].Word.ToLower(), xmlDatabasePath + "SAnaphoraXMLdatabase.xml"))
                    {

                        // goc: if (ssSen[1].Word.Equals("là"))

                        //{
                        senresult = new sentence_tagger(ssSen.SentenceLength + ssPresen.SentenceLength - 2);
                        int i = 0;
                        for (i = 0; i < k; i++)
                        {
                            senresult[i].Word_text = ssSen[i].Word;
                            senresult[i].Word_pos = ssSen[i].Pos;
                        }
                        int j = k;
                        int n = 0;
                        // noi 2 cau thanh 1
                        for (j = k; j < ssPresen.SentenceLength + k -1 ; j++,n++)
                        {
                            senresult[j].Word_text = ssPresen[n].Word;
                            senresult[j].Word_pos = ssPresen[n].Pos;
                        }
                        int m = j;
                        k++;
                        for (m = j; m < ssSen.SentenceLength + ssPresen.SentenceLength - 2; m++, k++)
                        {
                            senresult[m].Word_text = ssSen[k].Word;
                            senresult[m].Word_pos = ssSen[k].Pos;
                        }
                        iresolve = true;
                        return senresult;
                        //}
                    }
                }
            
                //else
                //{
                    int[] allverb = ssSen.GetAllVerbPosition();
                    if (allverb.Length > 0)
                    {
                        senresult = new sentence_tagger(ssPresen.SentenceLength + allverb[0] + 1);
                        int x =0;
                        int y = 0;
                        int i = 0;

                        //Vinh them TH 2 cau de co sang S+nghi
                        //int x;
                        if (ssPresen.IsContain("nghĩ") || (ssPresen.IsContain("cho") && ssPresen.IsContain("rằng")) || ssPresen.IsContain("đoán") || ssPresen.IsContain("đoán là"))
                        {
                            y = x;
                            for (x = 0; x < ssPresen.SentenceLength; x++)
                            {
                                if (ssPresen[x].Word.Equals("nghĩ") || ssPresen[x].Word.Equals("cho") || ssPresen[x].Word.Equals("đoán") || ssPresen[x].Word.Equals("đoán là"))
                                {
                                    //Minh cho rằng là ngày mai trời sẽ mưa. Tôi cũng nghĩ thế.
                                    if (ssPresen[x + 2].Pos.Equals("C"))
                                    {
                                        y = x + 3;
                                        break;
                                    }
                                    //Minh cho rằng ngày mai trời sẽ mưa. Tôi cũng nghĩ thế.
                                    else if (ssPresen[x + 1].Pos.Equals("C"))
                                    {
                                        y = x + 2;
                                        break;
                                    }
                                    //Minh nghĩ ngày mai trời sẽ mưa. Tôi cũng nghĩ thế.
                                    else
                                    {
                                        y = x + 1;
                                        break;
                                    }
                                }
                            }
                            
                        }
                        //end
                        //senresult.Sentence_length = senresult.Sentence_length - y;

                        senresult = new sentence_tagger(ssSen.SentenceLength + ssPresen.SentenceLength - 2 - y);
                        for (i = 0; i < senresult.Sentence_length; i++)
                        {
                            if (i <= allverb[0])
                            {
                                senresult[i].Word_text = ssSen[i].Word;
                                senresult[i].Word_pos = ssSen[i].Pos;
                            }
                            else
                            {
                                
                                if (!ssPresen[i - allverb[0] - 1 + y].Pos.Equals("-")) senresult[i].Word_text = ssPresen[i - allverb[0] - 1 + y].Word;
                                senresult[i].Word_pos = ssPresen[i - allverb[0] - 1 + x].Pos;
                            }
                        }
                        senresult[senresult.Sentence_length - 1].Word_text = ssSen[ssSen.SentenceLength - 1].Word;
                        senresult[senresult.Sentence_length - 1].Word_pos = ssSen[ssSen.SentenceLength - 1].Pos;
                    }
                    iresolve = true;
                //}
                /*goc
                if ((ssSen[0].Word.ToLower().Equals("đó")) || (ssSen[0].Word.ToLower().Equals("đấy")))
                {
                    
                    if (ssSen[1].Word.Equals("là"))
                    
                    {
                        senresult = new sentence_tagger(ssSen.SentenceLength + ssPresen.SentenceLength - 2);
                        int i = 0;
                        for (i = 0; i < ssPresen.SentenceLength - 1; i++)
                        {
                            senresult[i].Word_text = ssPresen[i].Word;
                            senresult[i].Word_pos = ssPresen[i].Pos;
                        }
                        int j = i + 1;
                        // noi 2 cau thanh 1
                        for (j = i + 1; j < senresult.Sentence_length; j++)
                        {
                            senresult[j].Word_text = ssSen[j - i].Word;
                            senresult[i].Word_pos = ssSen[j - i].Pos;
                        }
                        iresolve = true;
                    }
                }
                else
                {
                    int[] allverb = ssSen.GetAllVerbPosition();
                    if (allverb.Length > 0)
                    {
                        senresult = new sentence_tagger(ssPresen.SentenceLength + allverb[0] + 1);
                        int i = 0;
                        for (i = 0; i < senresult.Sentence_length; i++)
                        {
                            if (i <= allverb[0])
                            {
                                senresult[i].Word_text = ssSen[i].Word;
                                senresult[i].Word_pos = ssSen[i].Pos;
                            }
                            else
                            {
                                if(!ssPresen[i-allverb[0]-1].Pos.Equals("-")) senresult[i].Word_text = ssPresen[i-allverb[0]-1].Word;
                                senresult[i].Word_pos = ssPresen[i-allverb[0]-1].Pos;
                            }
                        }
                        senresult[senresult.Sentence_length - 1].Word_text = ssSen[ssSen.SentenceLength - 1].Word;
                        senresult[senresult.Sentence_length - 1].Word_pos = ssSen[ssSen.SentenceLength - 1].Pos;
                    }
                    iresolve = true;
                }
                */
                return senresult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }

    public class OneAnaphoraResolve
    {
        static private bool iresolve;

        static public XMLNounConnection conn = new XMLNounConnection();

         public OneAnaphoraResolve()
        {
            iresolve = false;
        }

        static public bool Iresolve
        {
            get { return iresolve; }
        }

        static public sentence_tagger Resolve(SentenceStruct ssPresen, SentenceStruct ssSen)
        {
            try
            {
                iresolve = false;
                sentence_tagger resultSen = null;
                int[] allNc = ssSen.GetAllNcPosition();
                int[] allnoun = ssPresen.GetNounsPosition();
                int i = 0;
                int count = 0;
                if (allNc.Length > 0)
                {
                    if (allnoun.Length > 0)
                    {
                        for (i = 0; i < allNc.Length; i++)
                        {
                            int j = 0;
                            for (j = 0; j < allnoun.Length; j++)
                            {
                                if (allnoun[j] > 0)
                                {
                                    string strNc = conn.NcWord(ssPresen[allnoun[j]], "");
                                    //if (ssPresen[allnoun[j] - 1].Pos.Equals("Nc"))
                                    //{
                                        if (ssPresen[allnoun[j] - 1].Word.ToLower().Equals(ssSen[allNc[i]].Word.ToLower()))
                                        {
                                            count++;
                                            ssSen[allNc[i]].Word += " " + ssPresen[allnoun[j]].Word;
                                            break;
                                        }
                                        else
                                        {
                                            if (strNc.Contains(ssSen[allNc[i]].Word.ToLower()))
                                            {
                                                count++;
                                                ssSen[allNc[i]].Word += " " + ssPresen[allnoun[j]].Word;
                                                break;
                                            }
                                        }
                                    //}
                                }
                               
                            }
                        }

                    }
                    if (count == allNc.Length) iresolve = true;
                    if (iresolve)
                    {

                        resultSen = new sentence_tagger(ssSen.SentenceLength + count);
                        count = 0;
                        int ij = 0;
                        while (ij < resultSen.Sentence_length)
                        {
                            if (ssSen[ij - count].Pos.Equals("Nc"))
                            {
                                resultSen[ij].Word_text = ssSen[ij - count].Word.Split(' ')[0];
                                resultSen[ij].Word_pos = "Nc";
                                ij++;
                            }
                            else
                            {
                                if (ij > 0)
                                {
                                    if (ssSen[ij - count - 1].Pos.Equals("Nc"))
                                    {

                                        resultSen[ij].Word_pos = "N";
                                        resultSen[ij].Word_text = ssSen[ij - count - 1].Word.Substring(resultSen[ij - 1].Word_text.Length);
                                        count++;
                                        resultSen[ij + 1].Word_text = ssSen[ij - count + 1].Word;
                                        resultSen[ij + 1].Word_pos = ssSen[ij - count + 1].Pos;
                                        ij = ij + 2;
                                    }
                                    else
                                    {
                                        resultSen[ij].Word_pos = ssSen[ij - count].Pos;
                                        resultSen[ij].Word_text = ssSen[ij - count].Word;
                                        ij++;
                                    }
                                }
                                else
                                {
                                    resultSen[ij].Word_pos = ssSen[ij - count].Pos;
                                    resultSen[ij].Word_text = ssSen[ij - count].Word;
                                    ij++;
                                }
                            }
                        }

                    }
                    else
                        iresolve = false;
                }
                else
                {
                    iresolve = true;
                }
                 return resultSen;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }
}
